package com.example.actionsignin.mvp.view;

import android.support.v4.util.ArrayMap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.actionsignin.R;
import com.example.actionsignin.entity.DescribeEntity;
import com.example.actionsignin.entity.ImagesEntity;
import com.example.actionsignin.entity.UserEntity;
import com.example.actionsignin.http.HttpUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 参加活动人数详情
 */
public class JoinDescribeActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "JoinDescribeActivity";
    private ListView mListView;
    private List<Map<String,String>> mList = new ArrayList<>();
    private SimpleAdapter mAdapter;

    private String mAid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_describe);

        mAid = getIntent().getStringExtra("aid");

        initView();
    }

    private void initView() {
        findViewById(R.id.activity_join_describe_left).setOnClickListener(this);
        mListView = (ListView) findViewById(R.id.activity_join_describe_list);

        getData();
        mAdapter = new SimpleAdapter(this,mList,R.layout.list_item_join_describe,
                new String[]{"name"},
                new int[]{R.id.item_join_describe_name});

        mListView.setAdapter(mAdapter);
    }

    private void getData()
    {
        HttpUtil.onSend().onActionDescribe(mAid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DescribeEntity<UserEntity,ImagesEntity>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull DescribeEntity<UserEntity,ImagesEntity> userEntityDescribeEntity) {
                        for (int i = 0; i < userEntityDescribeEntity.getUsers().size(); i++) {
                            UserEntity userEntity = userEntityDescribeEntity.getUsers().get(i);

                            Map<String,String> map = new ArrayMap<String, String>();
                            map.put("name",userEntity.getName());

                            mList.add(map);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d(TAG, "onError: " + e);
                    }

                    @Override
                    public void onComplete() {
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.activity_join_describe_left:
                finish();
                break;
        }
    }
}
